package com.example.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class DemoActivity : AppCompatActivity() {

    val viewModel:DemoViewModel by viewModels<DemoViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_demo)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        lifecycleScope.launch {
            viewModel.channel.send(DemoIntent.login("a123456","zhaolin1"))
        }

        lifecycleScope.launch {
            viewModel.uiState.collect{
                when(it){
                    is DemoState.onError -> {ToastUtils.showLong(it.msg)}
                    DemoState.onLoading -> {
                        Log.d("sss", "onCreate:发起了登录请求 ")
                    }
                    is DemoState.onSuccess<*> -> {
                        ToastUtils.showLong(it.data.toString())
                    }
                }
            }
        }
    }
}